﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据表 ZSJ_11_LJ_Production 对应的数据应用功能类。
	/// </summary>
	public class ZSJ_11_LJ_ProductionApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private IZSJ_11_LJ_ProductionRepository service = new ZSJ_11_LJ_ProductionRepository();

        public List<ZSJ_11_LJ_ProductionEntity> GetList(Pagination pagination, string keyword, string combineNo, string extIds)
        {
            var expression = ExtLinq.True<ZSJ_11_LJ_ProductionEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FProductionNO.Contains(keyword));
            }
            string extWhere = "";
            if (!string.IsNullOrEmpty(extIds))
            {
                List<string> tmpArray = extIds.Split(',').ToList();
                string extIdsStr = "";
                foreach (string str in tmpArray)
                {
                    extIdsStr += "'" + str + "',";
                }
                extWhere = "FProductionNO not in (" + extIdsStr.TrimEnd(',') + ")";
            }
            else if (!string.IsNullOrEmpty(combineNo))
            {
                extWhere = "FProductionNO not in (SELECT FProductionNO FROM CLDE_31_TC_CLDEerialCombine WHERE CombineNO='" + combineNo + "')";
            }
            return service.FindList(expression, pagination, extWhere);
        }

        public ZSJ_11_LJ_ProductionEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public ZSJ_11_LJ_ProductionEntity GetItem(string FProductionNO)
        {
            var expression = ExtLinq.True<ZSJ_11_LJ_ProductionEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == FProductionNO);
            return service.FindEntity(expression);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.FProductionID == keyValue);
        }

        public void SubmitForm(ZSJ_11_LJ_ProductionEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.FProductionID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedByNo(string number)
        {
            var expression = ExtLinq.True<ZSJ_11_LJ_ProductionEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == number);
            ZSJ_11_LJ_ProductionEntity entity = service.FindEntity(expression);
            return entity == null;
        }

        public void DeleteFormByNumber(string number)
        { 
            var expression = ExtLinq.True<ZSJ_11_LJ_ProductionEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == number);
            service.Delete(expression);
        }
    }
}